
#include <X11/Xlib.h>
#include <stdio.h>
#include <assert.h>

void dump_display_info(Display* dpy)
{
    // Xサーバに関する情報を取得するマクロ
    printf("XServerVender = %s\n", XServerVendor(dpy));
    printf("DisplayString = %s\n", DisplayString(dpy));
    printf("DefaultScreen = %d\n", DefaultScreen(dpy));
    printf("DisplayWidth = %d\n", DisplayWidth(dpy, DefaultScreen(dpy)));
    printf("DisplayHeight = %d\n", DisplayHeight(dpy, DefaultScreen(dpy)));
    printf("DisplayWidthMM = %d\n", DisplayWidthMM(dpy, DefaultScreen(dpy)));
    printf("DisplayHeightMM = %d\n", DisplayHeightMM(dpy, DefaultScreen(dpy)));
    printf("ProtocolVersion = %d\n", ProtocolVersion(dpy));
    printf("ProtocolRevision = %d\n", ProtocolRevision(dpy));
    
    return;
}

int main(void)
{
    Display* dpy; // ディスプレイ-> Xサーバのこと
    Window root; // 
    Window win, win_2;
    int screen;
    unsigned long black, white;
    XEvent evt;

    Colormap cmap;
    XColor color, exact;
    unsigned long green;

    // Xサーバと接続する
    // 引数がNULLの場合は環境変数DISPLAYの値になる 
    dpy = XOpenDisplay(NULL);

    assert(dpy != NULL);

    dump_display_info(dpy);

    root = DefaultRootWindow(dpy);
    screen = DefaultScreen(dpy);

    cmap = DefaultColormap(dpy, screen);

    white = WhitePixel(dpy, screen);
    black = BlackPixel(dpy, screen);

    XAllocNamedColor(dpy, cmap, "green", &color, &exact);
    green = color.pixel;

    win = XCreateSimpleWindow(dpy, root, 100, 100, 100, 100, 2, black, green);

    // ウィンドウ2号はこの用に生成する
    win_2 = XCreateSimpleWindow(dpy, root, 200, 200, 100, 100, 2, black, green);

    XSelectInput(dpy, win, KeyPressMask);
    XSelectInput(dpy, win_2, KeyPressMask);

    // ウィンドウを可視化する
    XMapWindow(dpy, win);
    XMapWindow(dpy, win_2);

    while(1)
    {
        XNextEvent(dpy, &evt);

        switch(evt.type)
        {
            case KeyPress:
                XDestroyWindow(dpy, win);
                XCloseDisplay(dpy);
                return 0;
        }
    }

    return 0;
}

